package visor;

import java.io.File;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import static lector.Lector2.printNodeList;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Visor extends JFrame {

    JTree jArbol;

    public Visor() {
        this.setBounds(10, 10, 300, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jArbol = new JTree();
        this.add(jArbol);
    }

    public void cargarModelo(NodeList nodeList) {
        DefaultMutableTreeNode padre = new DefaultMutableTreeNode(nodeList.item(0).getNodeName(), true);
        DefaultTreeModel modelo = new DefaultTreeModel(padre, true);
        jArbol.setModel(modelo);
    }

    public void cargarJTree(DefaultMutableTreeNode padre, NodeList nodeList) {
        int index = 0;
        for (int count = 0; count < nodeList.getLength(); count++) {
            DefaultTreeModel modelo = new DefaultTreeModel(padre, true);
            Node tempNode = nodeList.item(count);
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                DefaultMutableTreeNode hijo = new DefaultMutableTreeNode(tempNode.getNodeName(), true);
                modelo.insertNodeInto(hijo, padre, index++);
                if (tempNode.hasChildNodes()) {
                    // loop again if has child nodes
                    cargarJTree(padre, tempNode.getChildNodes());
                }
            } else {
                DefaultMutableTreeNode hijo = new DefaultMutableTreeNode(tempNode.getTextContent(), false);
                modelo.insertNodeInto(hijo, padre, index);
            }
        }
    }
}
